package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.RefreshContent;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.WaitActivityResult;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * Created by zxh on 16/12/14.
 */

public class MyAdviceModel extends XListModel implements WaitActivityResult, RefreshContent {
    private boolean advice;
    private boolean unreply;
    private boolean expert;

    @Bindable
    public boolean isExpert() {
        return expert;
    }

    @Bindable
    public boolean isAdvice() {
        return advice;
    }

    public void setAdvice(boolean advice) {
        this.advice = advice;
        if (this.advice) {
            setUnreply(false);
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
        notifyPropertyChanged(BR.advice);
    }

    @Bindable
    public boolean isUnreply() {
        return unreply;
    }

    public void setUnreply(boolean unreply) {
        this.unreply = unreply;
        if (this.unreply) {
            setAdvice(false);
            if (!this.expert) {
                DialogModel model = new DialogModel(getContext());
                PopupActivity.startActivityForResult((Activity) getContext(), R.layout.v_dialog_apply_expert,
                        HomeActivity.REQUEST_CONFIRM_APPLY_EXPERT, BR.model, model);
            } else {
                getAdapter().clear(true);
                setCurrentPage(1);
                load(1);
            }
        }
        notifyPropertyChanged(BR.unreply);
    }

    public MyAdviceModel(Context conetxt, boolean isExpert) {
        super(conetxt);
        super.getHandler().setTitle("我的咨询");
        getAdapter().setVariableId(BR.adviceInfo);
        this.expert = isExpert;
        advice = true;
    }

    @Override
    public void load(final int page) {
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
// "consultid":"32","expertid":"1","uid":"1",
// "question":"http:\/\/dmyjr.oss-cn-beijing.aliyuncs.com\/17073c16-bc69-4204-99a2-37127a9947cb.amr",
// "systime":"2016\/12\/13 20:27:38","payflag":"0","expertname":"null","status":"未回复"
                XListAdapterInterface adapter = getAdapter();
                Context context = getContext();
                ArrayList list = null;

                try {
                    if (advice) {
                        list = WebApi.myExpertAsk(getContext(), page);
                        for (Object item : list) {
                            LinkedTreeMap map = (LinkedTreeMap) item;
                            AdviceListItem adviceItem = new AdviceListItem(context, R.layout.i_myadvice);
                            adviceItem.setAllowDelete(true);
                            adviceItem.setSystime(Utils.getDateFromFormat(map.get("systime"), "yyyy/MM/dd HH:mm:ss"));
                            adviceItem.setAdviceId(Utils.getIntValue(map.get("consultid")));
                            adviceItem.setExpertId(Utils.getIntValue(map.get("expertid")));
                            adviceItem.setUid(Utils.getIntValue(map.get("uid")));
                            adviceItem.setQuestion((String) map.get("question"));
                            adviceItem.setPayflag(Utils.getBooleanValue(map.get("payflag")));
                            adviceItem.setStatus((String) map.get("status"));
                            adviceItem.setName((String) map.get("expertname"));
                            adviceItem.setExpert(false);
                            adapter.add(adviceItem);
                        }
                    } else {
                        list = WebApi.myExpertAnswer(getContext(), page);
                        for (Object item : list) {
                            LinkedTreeMap map = (LinkedTreeMap) item;
                            AdviceListItem adviceItem = new AdviceListItem(context, R.layout.i_myadvice);
                            adviceItem.setAllowDelete(false);
                            adviceItem.setSystime(Utils.getDateFromFormat(map.get("systime"), "yyyy/MM/dd HH:mm:ss"));
                            adviceItem.setAdviceId(Utils.getIntValue(map.get("consultid")));
                            adviceItem.setExpertId(Utils.getIntValue(map.get("expertid")));
                            adviceItem.setUid(Utils.getIntValue(map.get("uid")));
                            adviceItem.setQuestion((String) map.get("question"));
                            adviceItem.setPayflag(Utils.getBooleanValue(map.get("payflag")));
                            adviceItem.setStatus((String) map.get("status"));
                            adviceItem.setName((String) map.get("username"));
                            adviceItem.setExpert(true);
//                    adviceItem.setExpertName((String)map.get(""));
                            adapter.add(adviceItem);
                        }
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                if (list.size() > 0) {
                    setCurrentPage(page);
                }
                notifyDataSetChanged(list);
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == HomeActivity.REQUEST_CONFIRM_APPLY_EXPERT) {
                HomeActivity.beginApplyExpert(getContext());
            }
        }
    }

    @Override
    public void refresh() {
        getAdapter().clear(true);
        this.setCurrentPage(1);
        this.load(1);
    }
}
